package com.thinvent.nj.ais.controller.sys;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.thinvent.nj.common.rest.ResponseEntity;
import com.thinvent.nj.common.util.CollectionUtil;
import com.thinvent.nj.common.util.MapperUtil;
import com.thinvent.nj.common.util.StringUtil;
import com.thinvent.nj.ais.entity.Org;
import com.thinvent.nj.ais.entity.User;
import com.thinvent.nj.ais.service.OrgService;
import com.thinvent.nj.ais.service.UserService;
import com.thinvent.nj.web.controller.BaseViewController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 组织管理controller
 * @author liupeijun
 */
@Controller
@RequestMapping(path = "/sys/orgs")
public class OrgController extends BaseViewController {

    @Autowired
    private OrgService orgService;

    @Autowired
    private UserService userService;

    @RequiresPermissions("org:view")
    @RequestMapping(method = RequestMethod.GET)
    public String toHtml() {
        return "sys/orgList";
    }

    @RequiresPermissions("org:view")
    @RequestMapping(path = "/search/page", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity findTableData(@RequestBody Map<String, Object> params) {
        List<Org> targets = orgService.getAllOrgListByPId("-1", getQueryMsg(params));
        JSONObject jsonObject = new JSONObject(1);
        jsonObject.put("result", targets);

        return ResponseEntity.ok(jsonObject);
    }

    @RequiresPermissions("org:view")
    @RequestMapping(path = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity get(@PathVariable("id") String id) {
        return ResponseEntity.ok(orgService.get(id));
    }

    @RequiresPermissions("org:add")
    @RequestMapping(method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity insert(@RequestBody Map<String, Object> params) {
        Org target = new Org();
        MapperUtil.copy(params, target);

        if (StringUtil.isNullOrEmpty(target.getPId())) {
            target.setPId("-1");
        }

        orgService.insert(target);

        return ResponseEntity.ok();
    }

    @RequiresPermissions("org:update")
    @RequestMapping(path = "/{id}", method = RequestMethod.PUT)
    @ResponseBody
    public ResponseEntity update(@PathVariable("id") String id, @RequestBody Map<String, Object> params) {
        Org target = orgService.get(id);
        MapperUtil.copy(params, target);

        if (StringUtil.isNullOrEmpty(target.getPId())) {
            target.setPId("-1");
        }

        orgService.update(target);

        return ResponseEntity.ok();
    }

    @RequiresPermissions("org:delete")
    @RequestMapping(path = "/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public ResponseEntity del(@PathVariable("id") String id) {
        orgService.delete(id);
        return ResponseEntity.ok();
    }

    @RequiresPermissions("org:view")
    @RequestMapping(path = "/{pId}/tree", method = RequestMethod.POST)
    public void getOrgTree(@PathVariable("pId") String pId) {
        List<Org> target = orgService.getAllOrgListByPId(pId, Maps.newHashMap());

        render(CollectionUtil.listToZtree(MapperUtil.convertToListMap(target), "id", "pId", "fullName"));
    }

    @RequestMapping(path = "/{id}/users", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity getUsers(@PathVariable("id") String orgId) {
        List<User> users = userService.findByOrgId(orgId);

        return ResponseEntity.ok(users);
    }
}
